<template>
  <div class="app-container">
    <el-form ref="form" :model="user" :rules="rules" label-width="100px">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input  type="password" v-model="user.oldPassword" placeholder="请输入旧密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input  type="password" v-model="user.newPassword"  placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input  type="password" v-model="user.confirmPassword"  placeholder="请输入确认密码"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" size="mini" @click="doSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import sysUserApi from "@/api/system/sysUser";

export default {
  name: "userInfo",
  data(){
    const equalToPassword=(rule,value,callback)=>{
      if(this.user.newPassword!==value){
        callback(new Error("两次密码不致"))
      }else{
        callback()
      }
    }
    return{
      user:{
        oldPassword:undefined,
        newPassword:undefined,
        confirmPassword:undefined
      },
      rules:{
        oldPassword: [
          {required: true, message: '旧密码不能为空', trigger: 'blur'}
        ],
        newPassword: [
          {required: true, message: '新密码不能为空', trigger: 'blur'},
          { min: 6, max: 20, message: '新密码必须是 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          {required: true, message: '确认密码不能为空', trigger: 'blur'},
          {required: true,validator:equalToPassword, trigger: 'blur'},
        ],
      }
    }
  },
  methods:{
    doSubmit() {
      this.$refs["form"].validate(valid=>{
        if(valid){
          let data={oldPassword:this.user.oldPassword,newPassword:this.user.newPassword}
          sysUserApi.updateUserPwd(data).then(res=>{
            this.$message.success("修改成功")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
